<?php

class ZendBlog_Validate_Authorise extends Zend_Validate_Abstract {
    const NOT_AUTHORISED = 'notAuthorised';

    protected $_authAdapter;
    protected $_messageTemplate = array(
        self::NOT_AUTHORISED => 'No user exists'
    );

    public function getAuthAdapter() {
        return $this->_authAdapter;
    }

    public function isValid($value, $context = null) {
        $value = (string) $value;
        $this->_setValue($value);

        if (is_array($context)) {
            if (!isset($context['password'])) {
                return false;
            }
        }

        $this->_authAdapter= new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
        $this->_authAdapter->setTableName('authors')
                           ->setIdentityColumn('username')
                           ->setCredentialColumn('password')
                           ->setIdentity($values['name'])
                           ->setCredential(hash('SHA256', $values['password']));
        $auth = Zend_Auth::getInstance();
        $result = $auth->authenticate($this->_authAdapter);
        if($result->isValid()){
            $this->_error(self::NOT_AUTHORISED);
            return false;
        }
        return true;
    }

}

?>
